<?php

class C_CanLamSang extends Controller {
			
	public function C_CanLamSang($template_mod = 'general') {
		parent::Controller();
		$this->template_mod = $template_mod;
		$this->view->path = 'can_lam_sang';				
	}
	
	public function actionLoadCanLamSangAjax() {	
	    $recordId  =   $_POST['recordId'];   
	    $this->assign_by_ref('cls', Celini::newOrdo('EmrCanLamSang')->getObjectFromRecordId($recordId));     	   
	    $this->assign('recordId', $recordId);
		return $this->view->render('edit_can_lam_sang.html');		
	}
	
	public function actionEditCanLamSangAjax() {
	    $emr = $_POST['cls'];
       
        $emr['sieu_am']     =   Utils::sanitizeText($emr['sieu_am'], false);
        $emr['xet_nghiem']   =   Utils::sanitizeText($emr['xet_nghiem'], false);
        $emr['x_quang']        =   Utils::sanitizeText($emr['x_quang'], false);
        $emr['ghi_chu']      =   Utils::sanitizeText($emr['ghi_chu'], false);        
        
        $enId   =(int)  $_POST['encounterId'];
        $encounter  =   Celini::newORDO('EmrCanLamSang', $enId);        

        $emr['medical_record_id'] = $_POST['recordId'];
        $encounter->populateArray($emr);
        $encounter->persist();
            
        return $encounter->get_id();               
	}
	
	public function actionLoadKetQuaCLSAjax() {
	    $recordId  =   $_POST['recordId'];        
        $this->assign_by_ref('cls', Celini::newOrdo('EmrCanLamSang')->getObjectFromRecordId($recordId));
        $this->assign('recordId', $recordId);
        return $this->view->render('ket_qua_cls.html');
	}
	
	public function actionLoadChiDinhCLSAjax() {
	    $recordId  =   $_POST['recordId'];
        $encounter =   ORDataObject::factory('EmrGeneralExamination2')->getObjectFromRecordId($recordId);
        $diagnosis = ''; 
        if($encounter->get_id() > 0){
            $doctorName =   $encounter->getDoctorName();   
            $diagnosis = $encounter->get('diagnosis');                    
        } else {
            $person     = ORDataObject::factory("Employee");
            $person->setup($this->_me->get_person_id());
            $doctorName =   $person->getName();           
        }
        $this->assign('doctor', $doctorName);
        $this->assign('diagnosis', $diagnosis);
	    return $this->view->render('chi_dinh_cls.html');
	}
	
	public function actionGetLabCodeAutocompleteAjax() {
	    $q = $_GET['q'];
	    if (!$q) return;
	    $q = strtoupper($q);
	    $result = Celini::newORDO('LabService')->getLabAutoCompleteByCode($q);
	    foreach ($result as $item) {	        
	        echo $item['code'] . '|' . $item['service_name'] . '|' . $item['service_id'] . '|' . $item['lab_group_id'] . '|' . $item['lab_group_name'] . "\n";
	    }
	}
	
	public function actionGetLabNameAutocompleteAjax() {
	$q = $_GET['q'];
        if (!$q) return;
        
        $result = Celini::newORDO('LabService')->getLabAutoCompleteByName($q);
        foreach ($result as $item) {            
            echo $item['code'] . '|' . $item['service_name'] . '|' . $item['service_id'] . '|' . $item['lab_group_id'] . '|' . $item['lab_group_name'] . "\n";
        }
	}
	
	public function actionPrintPhieuDienCo() {
	    $recordId = $_GET['recordId'];
	    $data = $_GET['data'];
	    
	    $this->assignPrintCommonInfo($recordId, $data);	    
	    return $this->view->render('print_dien_co.html');
	}
	
	public function actionPrintPhieuDienNao(){
	    $recordId = $_GET['recordId'];
        $data = $_GET['data'];
        
        $this->assignPrintCommonInfo($recordId, $data);                      
        return $this->view->render('print_dien_nao.html');
	}
	
	public function actionPrintPhieuSieuAm() {
	    $recordId = $_GET['recordId'];
        $data = $_GET['data'];
        
        $this->assignPrintCommonInfo($recordId, $data);
        
        $chidinh['bung_tong_quat']  = (isset($data['bung_tong_quat']) && $data['bung_tong_quat'] == 'on')?1:0;
        $chidinh['tuyen_giap']      = (isset($data['tuyen_giap']) && $data['tuyen_giap'] == 'on')?1:0;
        $chidinh['dau_do']          = (isset($data['dau_do']) && $data['dau_do'] == 'on')?1:0;
        $chidinh['tim_doppler_mau'] = (isset($data['tim_doppler_mau']) && $data['tim_doppler_mau'] == 'on')?1:0;
        $chidinh['tuyen_vu']        = (isset($data['tuyen_vu']) && $data['tuyen_vu'] == 'on')?1:0;               

        $this->assign('chidinh', $chidinh);
        return $this->view->render('print_sieu_am.html');
	}
	
	public function actionPrintPhieuXetNghiem() {
        $recordId = $_GET['recordId'];
        $data = $_GET['data'];
                
        $this->assignPrintCommonInfo($recordId, $data);   
                                     
        $labGroup = Celini::newOrdo('LabGroup')->loadLabGroups();        
        $result = array();
        foreach ($labGroup as $group) {
            $key = $group['lab_group_id'];
            $name = $group['name'];
            
            if (isset($_GET['labgroup' . $key])) {
                $labData = $_GET['labgroup' . $key];                
                $tmp = array();                
                foreach ($labData as $labItem) {
                    $tmp[] = $labItem;
                }
                $result[$key]['name'] = $name;
                $result[$key]['items'] = $tmp;
            }
        }
                
        $this->assign('selectedLabs', $result);
	    return $this->view->render('print_xet_nghiem.html');
	}
	
	public function actionPrintPhieuChuyenKhoa() {
	    $recordId = $_GET['recordId'];
        $data = $_GET['data'];
        
        $this->assignPrintCommonInfo($recordId, $data);                      
        return $this->view->render('print_chuyen_khoa.html');
	} 
	
	public function actionPrintPhieuChiDinh() {
	    $recordId = $_GET['recordId'];
        $data = $_GET['data'];
        
        $this->assignPrintCommonInfo($recordId, $data);                      
        return $this->view->render('print_chi_dinh.html');
	}
	
	/**
	 * Assign common print info to print view
	 * 
	 * @param int $recordId Medical Record Id
	 * @param array $data
	 *     [doctor]
	 *     [diagnosis]
	 *     [request]	 
	 */
	private function assignPrintCommonInfo($recordId, $data) {
	    $patientId = Celini::newOrdo('MedicalRecord', $recordId)->get('patient_id');

        $this->assign_by_ref('patientObj', Celini::newOrdo('Patient', $patientId));
        $this->assign_by_ref('person', Celini::newOrdo('Person', $patientId));
        $this->assign('doctor', $data['doctor']);
        $this->assign('diagnosis', $data['diagnosis']);
        $this->assign('request', $data['request']);
        
        $config    =   Celini::configInstance();
        $hospitalInfo = $config->get('hospital_info');
        $this->assign('hospitalInfo', $hospitalInfo['vi_VN']);
        
        $this->assign('date', date('d/m/Y'));  
	}
}
?>